/*
** exec.c for mysh in /home/nadali_t//local/svn/tek2ed/minishell1
** 
** Made by thomas nadalie
** Login   <nadali_t@epitech.net>
** 
** Started on  Thu Nov  8 12:23:51 2012 thomas nadalie
** Last update Thu Nov  8 12:25:46 2012 thomas nadalie
*/

#include	<sys/types.h>
#include	<sys/wait.h>
#include	<stdlib.h>
#include	<unistd.h>
#include	"my.h"

char		**exec_cmd(char **path, char **tab, char **env)
{
  pid_t		pid;
  int		i;
  t_list	*list;

  i = 1;
  init_struct(&list);
  path = concat_path(path, tab[0]);
  if ((env = catch_builtins(tab, env, list)) == NULL)
    return (NULL);
  pid = fork();
  if (pid == -1)
    return (env);
  if (pid == 0)
    {
      if (list->stop != 1)
	{
	  while (execve(path[i], tab, env) == -1 && list->end != 1)
	    {
	      if (path[i++] == NULL)
		{
		  list->end = 1;
		  cmd_error(tab);
		  return (NULL);
		}
	    }
	}
    }
  else
    wait(&pid);
  return (env);
}
